package swu.edu.cn.javaee.controller;

import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import swu.edu.cn.javaee.entity.Group;
import swu.edu.cn.javaee.entity.GroupInfo;
import swu.edu.cn.javaee.service.GroupService;
import static swu.edu.cn.javaee.component.Signal.*;

@Controller
//解决跨域问题：允许所有源域名访问
@CrossOrigin(origins = "*")

public class GroupController {
    @Resource
    private GroupService groupService;

    //创建群
    @RequestMapping("/saveGroup")
    @ResponseBody
    public String saveGroup(String gname){
        String result = groupService.saveGroup(gname);
        if (result!=null){
            return result;
        }
        else{
            return FAILED_CODE;
        }
    }

    //遍历所有群
    @RequestMapping("/queryGroupList")
    @ResponseBody
    public GroupInfo queryGroupList(){
        GroupInfo groupInfo=groupService.queryGroupList();
        if (groupInfo!=null){
            System.out.println(groupInfo);
            return groupInfo;
        }
        else {
            return null;
        }
    }

    //根据id查询群
    @RequestMapping("/queryGroupById")
    @ResponseBody
    public Group queryGroupById(String gid){
        Group result=groupService.queryGroupById(gid);
        if (result!=null){
            System.out.println(result);
            return result;
        }
        else {
            return null;
        }
    }

    //推荐群
    @RequestMapping("/recommend")
    @ResponseBody
    public String recommend(String gid){
        String result=groupService.recommend(gid);
        return result;
    }

    //避雷群
    @RequestMapping("/not_recommend")
    @ResponseBody
    public String not_recommend(String gid){
        String result =groupService.not_recommend(gid);
        return result;
    }

    //删除群
    @RequestMapping("/deleteGroup")
    @ResponseBody
    public String deleteGroup(String gid){
        String result=groupService.deleteGroup(gid);
        return result;
    }
}
